import type MarkdownIt from 'markdown-it'

export function alterUserPlugin(md: MarkdownIt) {
  const defaultRender = md.renderer.rules.fence || function(tokens, idx, options, env, self) {
    return self.renderToken(tokens, idx, options)
  }

  md.renderer.rules.fence = function (tokens, idx, options, env, self) {
    const token = tokens[idx]
    const code = token.content.trim()
    
    // 兼容现有的 ebnf+diagram 语法
    if (token.info.trim() === 'ebnf+diagram') {
      const escapedContent = md.utils.escapeHtml(token.content)
      return `<AlterUserBlock :content="\`${escapedContent.replace(/`/g, '\\`')}\`" />`
    }
    
    // 检查是否以 # ALTER 开头（新语法）
    if (code.startsWith('# ALTER')) {
      const escapedContent = md.utils.escapeHtml(token.content)
      return `<AlterUserBlock :content="\`${escapedContent.replace(/`/g, '\\`')}\`" />`
    }
    
    return defaultRender(tokens, idx, options, env, self)
  }
} 